package s

import "fmt"

var (
	BAR = Dict[Str, int]{}
)

func (self Str) BarPrint(tag Str) Str {
	rw.Lock()
	line, ok := BAR[tag]

	if !ok {
		line = BAR.Len() + 1
		BAR[tag] = BAR.Len() + 1
	}
	rw.Unlock()
	// fmt.Println("row :", line)
	Str("").ANSISave().ANSICursor(line, 0).ANSIClearThisLine().A(self.Strip()).ANSIRestore().Print()
	return self
}

/* BarShowSize
use {key} {speed} {size}
example : Str("[{key}] : speed : {speed}  | size : {size} "
*/
func (self Str) BarShowSize(tag Str, usedSec float64, size int64, finish bool, allSize ...int64) Str {
	c := "B"
	cp := "B"
	cf := float64(size)
	speedSize := cf / usedSec
	if size > 1024*1024*1024 {

		c = "GB"
		cf = cf / (1024 * 1024 * 1024)
		// fmt.Println(c, cf)

	} else if size > 1024*1024 {
		c = "MB"
		cf = cf / (1024 * 1024)
		// fmt.Println(c, cf)

	} else if size > 1024 {
		c = "KB"
		cf = cf / 1024
		// fmt.Println(c, cf)
	}

	if speedSize > 1024*1024*1024 {

		cp = "GB"
		speedSize = speedSize / (1024 * 1024 * 1024)
		// fmt.Println(c, cf)

	} else if speedSize > 1024*1024 {
		cp = "MB"
		speedSize = speedSize / (1024 * 1024)
		// fmt.Println(c, cf)

	} else if speedSize > 1024 {
		cp = "KB"
		speedSize = speedSize / 1024
		// fmt.Println(c, cf)
	}

	SizeStr := fmt.Sprintf("%.2f%s", cf, c)
	speedStr := fmt.Sprintf("%.2f%s/sec", speedSize, cp)

	// self.Strip().ANSISelected() + Str(
	if !self.Has("{") {
		self = Str("[{key}]").ANSIBold()
		self += Str("| speed: {speed} | size: {size} ").ANSIGreen().ANSIItalic()
		self += Str("| used: {time}s ").ANSIYellow().ANSIUnderline()
	}

	if self.Has("{key}") {
		self = self.Format(Dict[Str, any]{
			"key": tag,
		})
	}

	if self.Has("{speed}") {
		self = self.Format(Dict[Str, any]{
			"speed": speedStr,
		})
	}

	if self.Has("{size}") {
		self = self.Format(Dict[Str, any]{
			"size": SizeStr,
		})
	}
	if self.Has("{time}") {
		self = self.Format(Dict[Str, any]{
			"time": fmt.Sprintf("%.2f", usedSec),
		})
	}
	if allSize != nil && size > 0 {
		per := float64(size*100) / float64(allSize[0])
		self = Sprintf("(%%%.2f) - ", per).ANSIGreen() + self
	}
	if finish {
		self = Str("(finish) - ").ANSIGreen().ANSIBold().ANSIItalic().ANSIBlink() + self
	}
	return self.BarPrint(tag)
}
